import time
import json
import csv

'''
将冷库数据写入到excel
'''


def main():
    print("开始爬取冷库数据" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    f = open("E:\cold_storage\codeStorageData.txt", encoding="utf-8")
    data = f.read()
    # print(data)
    jsonData = json.loads(data, encoding='utf-8')
    dataArray = jsonData['Tag']
    # print(dataArray)

    #  1.创建文件对象
    csvf = open('E:\cold_storage\csv_file_20221110.csv', 'w', newline="", encoding='utf-8')
    #
    # #  2.基于文件对象构建csv写入对象
    csv_write = csv.writer(csvf)
    #
    # #  3.构建列表头
    # title = ['冷库名称', '冷库地址', '成立时间', '园区描述', '联系电话', "制冷剂类型", "有无温度记录", "有无理货区", "有无封闭月台",
    #          "冷库面积", "存放商品品类", "冷库简介",
    #          "冷库楼层", "租仓类型", "经营类型", "存储类型", "仓库类型", '图片地址']
    # title = ['name', 'address', 'build_date', 'introduction', 'tel', 'longitude', 'latitude','country_name','province_name', 'city_name', 'region_name',
    #          # "制冷剂类型", "有无温度记录", "有无理货区", "有无封闭月台","冷库面积", "存放商品品类",
    #          "introduction",
    #          # "冷库楼层",
    #          "租仓类型", "经营类型", "存储类型", "仓库类型", '图片地址']

    title = ["name", "仓库类型", "经营类型", "制冷剂类型", "存储类型", "货物类型", "租仓类型"]

    # csv_write.writerow(['学号', '班级', '姓名'])
    csv_write.writerow(title)
    #
    # #  4.写入csv文件
    # csv_write.writerow(['001', '2021.1', '张三'])
    # csv_write.writerow(['002', '2021.1', '李四'])
    # csv_write.writerow(['003', '2021.1', '王五'])
    # csv_write.writerow(['004', '2021.1', '老六'])


    for item in dataArray:
        # print(item)
        targetArr = []
        targetArr.append(item['name'])
        # targetArr.append(item['address'])
        # targetArr.append(item['underTitle'][0])
        # targetArr.append(item['underTitle'][1])
        # targetArr.append(item['tel'])
        # targetArr.append(item['longitude'])
        # targetArr.append(item['latitude'])
        # targetArr.append(item['countryName'])
        # targetArr.append(item['provinceName'])
        # targetArr.append(item['cityName'])
        # targetArr.append(item['regionName'])


        # 设施详情
        targetArr.append(item['warehouseTypeName'])
        targetArr.append(item['businessTypeName'])
        targetArr.append(item['refrigerantType'])
        targetArr.append(item['storeTypeName'])
        targetArr.append(item['limitproducts'])
        # targetArr.append(item['havaTemperatureRecord'])
        # targetArr.append(item['haveCargoArea'])
        # targetArr.append(item['haveClosePlatform'])
        # targetArr.append(item['areaName'])
        # targetArr.append(item['limitproducts'])

        # 简介
        # targetArr.append(item['simpleInfo'])
        # targetArr.append(item['content'])
        # targetArr.append(item['floor'])

        for key, value in item.items():
            v = ""
            if value is not None:
                v = str(value)

            typeName = "-"
            if key == 'warehouseTypeRelationDtoList':
                warehouseTypeRelationDtoList = eval(v)
                for dto in warehouseTypeRelationDtoList:
                    relationType = ''
                    typeName = dto['baseType']
                    for dto1 in dto["warehouseTypeRelationList"]:
                        relationType += dto1['type'] + "|"

                    targetArr.append(relationType)
                    # print(relationType)

                    # relationType += "\n\r"

        # images = item['swiper']
        # imageUrl = ""
        # for img in images:
        #     imageUrl+= img +"\r\n"
        #
        # targetArr.append(imageUrl)
        print(targetArr)
        csv_write.writerow(targetArr)
        targetArr.clear()



    #  5.关闭文件
    csvf.close()




    print("冷库数据爬取完成" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


if __name__ == '__main__':
    main()
